<template>
  <div>
    <el-form ref="form" :rules="rules" :model="formData" label-width="100px">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="设备属性设置" name="first">
          <el-row>
            <el-col :span="12">
              <el-form-item label="计划名称" prop="name">
                <el-input v-model="formData.name" placeholder="计划名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开始时间" prop="starttime">
                <el-date-picker
                  v-model="formData.starttime"
                  style="width:100%"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择日期时间"
                  align="center"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="灌溉设备" prop="serialno">
                <el-select v-model="formData.serialno" clearable filterable placeholder="请选择">
                  <el-option
                    v-for="(item,index) in sprinklers"
                    :key="index"
                    :label="item.name + ' —— ' + item.serialno"
                    :value="item.serialno"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="启动延时" prop="delay">
                <el-input v-model="formData.delay" placeholder="启动延时">
                  <div slot="append">分钟</div>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="行进方向" prop="direction">
                <el-select v-model="formData.direction" clearable filterable placeholder="请选择">
                  <el-option
                    v-for="(item,index) in directions"
                    :key="index"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="停止角度" prop="delay">
                <el-slider
                  v-model="formData.endAngle"
                  :max="Number(360)"
                  show-input
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="水泵联动" prop="total_fee">
            <div style="display:flex;flex-direction:row;">
              <el-checkbox v-model="formData.isPumped" border>关联水泵</el-checkbox>
              <el-select v-if="formData.isPumped" v-model="formData.pump.serialno" filterable clearable style="margin-left:12px;">
                <el-option
                  v-for="(item,index) in pumps"
                  :key="index"
                  :label="item.name"
                  :value="item.serialno"
                />
              </el-select>
            </div>
          </el-form-item>
          <el-row v-if="formData.isPumped">
            <el-col :span="12">
              <el-form-item label="泵启动时长" prop="direction">
                <el-input v-model="formData.pump.time" placeholder="启动延时">
                  <div slot="append">分钟</div>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预启动" prop="delay">
                <el-input v-model="formData.pump.delay" placeholder="预启动">
                  <div slot="append">分钟</div>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="分区属性设置" name="second" :disabled="!formData.serialno">
          <el-table
            :data="cellsFormatter"
            border
            style="width: 100%;margin-bottom:20px;"
          >
            <el-table-column
              prop="id"
              label="大分区"
              align="center"
              width="100"
            />
            <el-table-column
              prop="idx"
              label="小分区"
              align="center"
              width="100"
            />
            <el-table-column
              prop="name"
              label="行走速率(%)"
              align="center"
            >
              <template v-slot="scope">
                <el-input-number
                  v-model="scope.row.v"
                  class="mini-input"
                  size="mini"
                  :min="1"
                  :max="100"
                  :controls="false"
                  placeholder="行走速率"
                />
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              label="脉冲周期(s)"
              align="center"
            >
              <template v-slot="scope">
                <el-input-number
                  v-model="scope.row.d"
                  class="mini-input"
                  size="mini"
                  :min="1"
                  :max="100"
                  :controls="false"
                  placeholder="脉冲周期"
                />
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              label="占空比(%)"
              align="center"
            >
              <template v-slot="scope">
                <el-input-number
                  v-model="scope.row.p"
                  class="mini-input"
                  size="mini"
                  :min="1"
                  :max="100"
                  :controls="false"
                  placeholder="占空比"
                />
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              label="尾枪"
              align="center"
              width="160"
            >
              <template v-slot="scope">
                <el-radio-group v-model="scope.row.endGun" size="mini">
                  <el-radio-button label="打开" />
                  <el-radio-button label="关闭" />
                </el-radio-group>
              </template>
            </el-table-column>
            <!-- <el-table-column
                prop="address"
                label="操作"
                align="center"
                width="60">
                <template slot-scope="scope">
                   <el-button size="mini" type="danger" icon="el-icon-delete" circle></el-button>
                </template>
              </el-table-column> -->
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <el-button type="primary" @click="submit('form')">提交</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { dclassCodeMap } from '@/utils/constant'
export default {
  props: {
    formData: {
      type: Object,
      default() {
        return {
          isPumped: false,
          isCelled: false,
          pump: {}
        }
      }
    }
  },
  data() {
    return {
      activeName: 'first',
      directions: [
        { name: '正向', value: 1 },
        { name: '反向', value: 2 }
      ],
      rules: {
        name: [
          { required: true, message: '请输入计划名称', trigger: 'blur' }
        ],
        starttime: [
          { required: true, message: '请选择开始时间', trigger: 'blur' }
        ],
        serialno: [
          { required: true, message: '请选择设备', trigger: 'blur' }
        ],
        direction: [
          { required: true, message: '请选择方向', trigger: 'blur' }
        ]
      },
      cellsFormatter: []
    }
  },
  computed: {
    ...mapGetters(['devices', 'cells']),
    sprinklers() {
      return this.devices.filter(val => {
        return +val.product_type === dclassCodeMap.pg
      })
    },
    pumps() {
      return this.devices.filter(val => {
        return +val.product_type === dclassCodeMap.sb
      })
    }
  },
  watch: {
    'formData.serialno': {
      handler(newVal) {
        if (newVal) {
          this.cellsFormatter = this.cells[newVal]
          this.cellsFormatter.map((val) => {
            val.v = 100
            val.d = 60
            val.p = 100
            return val
          })
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    reset() {
      this.$emit('reset')
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.formData.cells = this.cellsFormatter
          this.activeName = 'first'
          this.$emit('submit', this.formData)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleRateChange(row) {
      const rate = row.rate
      this.cells.map(item => {
        if (row.id === item.id) {
          return Object.assign(item, { rate: rate })
        }
      })
      console.log(this.cells)
    }
  }
}
</script>

<style lang="scss" scoped>
.mini-input{
  width:100px;
}
</style>
